var spider = require('./spider');
var cheerio = require('cheerio');
var url_node = require('url');
var Iconv = require('iconv-lite');


function sort(title) {
    title = title.replace(/(^\s*)|(\s*$)/g, "");
    let str = title.substring(0, 1).toUpperCase();

    // 英文字符，数字
    if (str >= 'A' && str <= 'Z') {
        return str;
    } else if (str >= 0 && str <= 9) {
        return '0-9';
    } else {

        var p = pinyin(str, { "style": "normal" })[0][0].toUpperCase();
        console.log(p);
        p = p.substring(0, 1).toUpperCase();


        console.log((p >= 'A') && (p <= 'Z'));

        if ((p >= 'A') && (p <= 'Z')) {
            return p;
        } else {
            return '其他';
        }


    }


}
// setting
var setting = {
    headers: {},
    start: {
        url: 'http://comic.kukudm.com/comictype/3_*PageReplace*.htm',
        // url: 'http://www.1kkk.com/manhua-jpkr-p*PageReplace*/',
        pageItem: 1,
        curPage: 1,
        totalPage: 4000,
        timeout: 5000
    },
    isFinish: function (_setting) {

        if (_setting.start.curPage > _setting.start.totalPage) {
            console.log('---spider all list---');
            return true;
        } else {
            return false;
        }
    },
    // 条件认证
    validate: function (data) {

        if (data.isFinish == true) {
            return true;
        } else {
            return false;
        }
    },
    errorHandel: function (err) {
        console.log('-------error catch---------');
        console.log(err);
        console.log(_setting.start.curPage + '/' + _setting.start.totalPage);

    },
    successHandel: function (_setting) {
        console.log('-------success ' + _setting.start.curPage + '/' + _setting.start.totalPage + '---------');
        _setting.start.curPage++;
        spider.start(_setting);
    },
    // 解析
    parse: {
        type: 'html',
        // charset: 'gb2312',
        handel: function (body, _setting, url) {

            // console.log(res);
            var urlObj = url_node.parse(url);
            var protocol = urlObj.protocol;
            var hostname = urlObj.hostname;

            var httpUrl = protocol + "//" + hostname;



            // var $ = cheerio.load(res);

            var data;
            switch (hostname) {
                case 'www.1kkk.com':
                    var res = body.toString('utf-8');
                    var $ = cheerio.load(res);
                    data = kkkHtml($, httpUrl);
                    break;
                case 'manhua.dmzj.com':
                    var res = body.toString('utf-8');
                    var $ = cheerio.load(res);
                    data = dmzjHtml($, httpUrl);
                    break;
                case 'comic.kukudm.com':
                    var res = Iconv.decode(body, 'gb2312').toString();
                    var $ = cheerio.load(res);
                    data = xinxinHtml($, httpUrl);
                    break;
                default:
                    console.log('没有匹配');
                    break;
            }

            data['domain'] = hostname;
            console.log(data);

            var page = 100;
             // 设置总页数/总个数
             _setting.start.totalPage = page;

            return data;
            // return false;
        }
    },
    mongdb: {
        database: 'manhua',
        collection: 'manhua',
        type: 'update',
        query: {}
    },
    // ,lastId:"5a409e5f51e70917cccbf596"
};

spider.init_d(setting);


// 动漫之家网站爬取
function dmzjHtml($, host) {

    var liArry = $('.tcaricature_new .tcaricature_block ul');
    // console.log(liArry);
    if (liArry.length == 0) {
        return false;
    }

    var data = [];

    for (var i = 0; i < liArry.length; i++) {
        var item = liArry.eq(i);

        data.push({
            title: item.find('li').eq(0).find('a').attr('title'),
            href: host + item.find('li').eq(0).find('a').attr('href'),
            img: item.find('li').eq(0).find('img').attr('src'),
            auth: item.find('li').eq(1).find('div').eq(0).find('a').attr('title'),
            auth_href: host + item.find('li').eq(1).find('div').eq(0).find('a').attr('href'),
            news: item.find('li').eq(1).find('div').eq(1).find('a').attr('title'),
            news_href: host + item.find('li').eq(1).find('div').eq(0).find('a').attr('href'),
            isFinish: item.find('li').eq(0).find('span').length > 0 ? true : false,
            letter: sort(_title),
            info: null,
            chapters: null

        });
    }


    return data;

}

// 1kkk网站爬取
function kkkHtml($, host) {
    var liArry = $('.main .kk2 .in_01 li');

    if (liArry.length == 0) {
        return false;
    }
    var data = [];
    for (var i = 0; i < liArry.length; i++) {
        var item = liArry.eq(i);
        var _title = item.find('a').eq(0).text().replace(/(^\s*)|(\s*$)/g, "");
        data.push({
            title: _title,
            href: host + item.find('a').eq(0).attr('href'),
            img: item.find('a').eq(0).find('img').attr('src'),
            auth: null,
            auth_href: null,
            news: item.find('.lj_h1 a').text(),
            news_href: host + item.find('.lj_h1 a').attr('href'),
            isFinish: null,
            updateTime: null,
            letter: sort(_title),
            info: null,
            chapters: null
        });
    }




    return data;
}


// 新新漫画
function xinxinHtml($, host) {


    var liArry = $('#comicmain dd');
    if (liArry.length == 0) {
        return false;
    }

    var data = [];

    for (var i = 0; i < liArry.length; i++) {
        var item = liArry.eq(i);
        var _title = item.find('a').eq(1).text().replace(/(^\s*)|(\s*$)/g, "");
        data.push({
            title: _title,
            href: host + item.find('a').eq(1).attr('href'),
            img: item.find('a').eq(0).find('img').attr('src'),
            auth: null,
            auth_href: null,
            news: null,
            news_href: null,
            isFinish: null,
            updateTime: item.text().replace(/(^\s*)|(\s*$)/g, ""),
            type: null,
            info: null,
            letter: sort(_title)
        });
    }

    return data;
}



